//
//  DDChoosePositionCell.m
//  DingDongZP
//
//  Created by Jyh on 16/5/15.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDChoosePositionCell.h"

@implementation DDChoosePositionCell

- (void) setDetail:(DDJobsDetail *)detail {
    
    _detail = detail;
    self.position.text =  _detail.jobtitle;

    self.salaryLabel.text = [NSString stringWithFormat:@"¥ %@",!_detail.salary?@"薪资":_detail.salary];
    self.cityLabel.attributedText = [self string:!_detail.city?@"城市":_detail.city  withIcon:[UIImage imageNamed:@"icon_address"]];
    self.experienceLabel.attributedText = [self string:!_detail.experience?@"经验":_detail.experience withIcon:[UIImage imageNamed:@"icon_experience"]];
    self.eduLabel.attributedText = [self string:!_detail.edu?@"学历":_detail.edu withIcon:[UIImage imageNamed:@"icon_education"]];

}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.contentView.backgroundColor = [UIColor colorWithRed:0.937 green:0.941 blue:0.945 alpha:1.000];
        self.bgView = [[UIView alloc]init];
        self.bgView.backgroundColor = [UIColor whiteColor];
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 5;
        [self.contentView addSubview:self.bgView];
        
        WEAKSELF
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf);
            make.left.equalTo(@(15));
            make.right.equalTo(weakSelf).offset(-15);
            make.bottom.equalTo(weakSelf).offset(-15);
        }];
        
        self.position = [[UILabel alloc]init];
        self.position.textColor = COLOR_FONT_MAIN_GREEN;
        self.position.font = [UIFont systemFontOfSize:18];
        [self.bgView addSubview:self.position];
        [self.position mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.bgView).offset(10);
            make.left.equalTo(@(15));
            make.right.equalTo(self.bgView).offset(-15);
            make.height.equalTo(@(35));
        }];
        
        
        self.salaryLabel = [[UILabel alloc]init];
        self.salaryLabel.textColor = COLOR_FONT_RED;
        self.salaryLabel.font = [UIFont systemFontOfSize:15];
        [self.bgView addSubview:self.salaryLabel];
        [self.salaryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self.position.mas_bottom);
            make.left.equalTo(@(15));
            make.height.equalTo(@(30));
        }];
        
        
        self.cityLabel = [[UILabel alloc]init];
        self.cityLabel.textColor = COLOR_FONT_DARK_GRAY;
        self.cityLabel.font = [UIFont systemFontOfSize:15];
        [self.bgView addSubview:self.cityLabel];
        [self.cityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self.position.mas_bottom);
            make.left.equalTo(self.salaryLabel.mas_right).offset(15);
            make.height.equalTo(@(30));
        }];
        
        self.experienceLabel = [[UILabel alloc]init];
        self.experienceLabel.textColor = COLOR_FONT_DARK_GRAY;
        self.experienceLabel.font = [UIFont systemFontOfSize:15];
        [self.bgView addSubview:self.experienceLabel];
        [self.experienceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self.position.mas_bottom);
            make.left.equalTo(self.cityLabel.mas_right).offset(15);
            make.height.equalTo(@(30));
        }];
        
        self.eduLabel = [[UILabel alloc]init];
        self.eduLabel.textColor = COLOR_FONT_DARK_GRAY;
        self.eduLabel.font = [UIFont systemFontOfSize:15];
        [self.bgView addSubview:self.eduLabel];
        [self.eduLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self.position.mas_bottom);
            make.left.equalTo(self.experienceLabel.mas_right).offset(15);
            make.height.equalTo(@(30));
        }];
        
    }
    return self;
}

+(instancetype)loadTableView:(UITableView *)tableView
{
    NSString *simpleIdentify = @"DDChoosePositionCell";
    DDChoosePositionCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentify];
    if(cell == nil)
    {
        cell = [[DDChoosePositionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentify];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (NSAttributedString *)string:(NSString *)string withIcon:(UIImage *)icon
{
    if (string == nil || icon == nil) {
        return [NSAttributedString new];
    }
    NSMutableAttributedString *titleAttributed = [[NSMutableAttributedString alloc] initWithString:string];
    [titleAttributed addAttribute:NSForegroundColorAttributeName
                            value:COLOR_FONT_DARK_GRAY
                            range:NSMakeRange(0,string.length)];
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    textAttachment.image = icon;
    
    textAttachment.bounds = CGRectMake(0, 0,12,12);
    [titleAttributed insertAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment] atIndex:0];
    [titleAttributed insertAttributedString:[[NSAttributedString alloc] initWithString:@" "] atIndex:1];
    return titleAttributed;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end

